package com.lk.springboot.controller;

import com.lk.springboot.utils.MD5Util;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lk
 * @date 2020/1/8 0008 19:04
 *          HttpServerletRequest 装请求信息的类
     *      HttpServerletRespionse 装响应信息的类
 */
@RestController
public class MyGetCookieController {

    //首先，想要获取Cookie信息，那么就得先有Cookie信息，这边我们自己从头开始，先弄个Cookie吧。

    @RequestMapping(value = "/setCookies", method = RequestMethod.GET)
    public String setCookies(HttpServletResponse response) {
        MD5Util md5Util = new MD5Util();
        String MD5CookieValue = md5Util.encode(response.getHeader("id"));
        Cookie cookie = new Cookie("sessionId", MD5CookieValue);
        response.addCookie(cookie);
        return "添加cookies信息成功";
    }

    //注解方式获取cookie中对应的key值
    @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue("sessionId") String sessionId) {
        //前提是已经创建了或者已经存在cookie了，那么下面这个就直接把对应的key值拿出来了。
        System.out.println("testCookieValue,sessionId=" + sessionId);

        return "SUCCESS";
    }


}


